package com.shk.service.imp;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shk.constant.RedisConstant;
import com.shk.dao.SetmealDao;
import com.shk.entity.PageResult;
import com.shk.pojo.Setmeal;
import com.shk.service.SetmealService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import redis.clients.jedis.JedisPool;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Value("${out_put_path}")
    private String outPutPath;

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) throws Exception {
        setmealDao.add(setmeal);

        setSetmealAndCheckGroup(setmeal.getId(), checkgroupIds);

        String fileName = setmeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, fileName);

        generateMobileStaticHtml();
    }

    @Override
    public PageResult findAll(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Setmeal> page = setmealDao.findAll(queryString);
        List<Setmeal> rows = page.getResult();
        long total = page.getTotal();
        return new PageResult(total, rows);
    }

    @Override
    public void delete(Integer id) {

        setmealDao.deleteSetmealAndCheckGroup(id);
        setmealDao.delete(id);

    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    @Override
    public List<Integer> findSetmealAndCheckGroup(Integer id) {
        return setmealDao.findSetmealAndCheckGroup(id);
    }

    @Override
    public void update(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.update(setmeal);
        setmealDao.deleteSetmealAndCheckGroup(setmeal.getId());
        setSetmealAndCheckGroup(setmeal.getId(), checkgroupIds);


    }


    /*建立套餐和检查组之间的关系*/
    public void setSetmealAndCheckGroup(Integer setmealid, Integer[] checkgroupIds) {
        if (checkgroupIds != null) {
            for (Integer checkgroupid : checkgroupIds) {
                Map map = new HashMap();
                map.put("setmeal_id", setmealid);
                map.put("checkgroup_id", checkgroupid);
                setmealDao.setSetmealAndCheckGroup(map);
            }
        }
    }




    /*移动端*/
    @Override
    public List<Setmeal> mobileFindAll() {
        List<Setmeal> list = setmealDao.mobileFindAll();
        return list;
    }

   /* @Override
    public Setmeal find(Integer id) {
        Setmeal setmeal = setmealDao.find(id);
        return setmeal;
    }*/


    public void generateMobileStaticHtml() throws Exception {
        List<Setmeal> list = setmealDao.mobileFindAll();
        generateMobileSetmealListHtml(list);
        generateMobileSetmealHtml(list);
    }

    /*套餐列表的静态页面*/
    public void generateMobileSetmealListHtml(List<Setmeal> list) throws Exception {
        Map map = new HashMap();
        map.put("setmealList", list);
        generalHtml("mobile_setmeal.ftl", "m_setmeal.html", map);
    }

    /*套餐详情页面  有个套餐一个详情页面 有多个*/
    public void generateMobileSetmealHtml(List<Setmeal> list) throws Exception {
        if (list != null) {
            for (Setmeal setmeal : list) {
                Setmeal data = setmealDao.findById(setmeal.getId());
                Map map = new HashMap();
                map.put("setmeal", data);
                generalHtml("m_setmeal_detail.ftl", "m_setmeal_detail_" + setmeal.getId() + ".html", map);
            }
        }

    }

    /*通用模板*/
    public void generalHtml(String templateName, String htmlName, Map map) throws Exception {

        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        Writer writer = null;
        try {
            Template template = configuration.getTemplate(templateName);
            File file = new File(outPutPath + "/" + htmlName);
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));

            template.process(map, writer);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }


    }



}